<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <script type="text/javascript" src="../lib/jquery/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
  </head>
  
  <body>
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="/welcome">首页</a>
        <a>
          <cite>导航元素</cite>
        </a>
      </span>
		<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
			<i class="layui-icon" style="line-height:30px">ဂ</i>
		</a>
    </div>
    <div class="x-body">
		<div class="layui-row">
		   <form class="layui-form layui-col-md12 x-so">
		     <input class="layui-input" placeholder="开始日" name="start_date" id="start_date">
		     <input class="layui-input" placeholder="截止日" name="end_date" id="end_date">
		     <input type="text" name="dwz_name"  placeholder="请输入短网址名称" class="layui-input">
		     <button class="layui-btn" lay-submit="" lay-filter="search"><i class="layui-icon">&#xe615;</i></button>
		   </form>
		</div>
		<xblock>
			<button class="layui-btn" onclick="x_admin_show('添加短网址','edit')"><i class="layui-icon"></i>添加</button>
        	<span id="count" class="x-right" style="line-height:40px">共有数据： 条</span>
		</xblock>
      	<table id="table" lay-filter="table"></table>
		<table class="layui-table layui-hide">
			<tbody>
				<tr>
					<td class="td-manage">
						<a title="编辑" lay-event="edit" href="javascript:;">
							<i class="layui-icon">&#xe642;</i>
						</a>&nbsp;
						<a title="删除" lay-event="del" href="javascript:;">
							<i class="layui-icon">&#xe640;</i>
						</a>
					</td>
				</tr>
			</tbody>
		</table>
    </div>
	<script>
	layui.use(['table', "form", "util"], function(){
		var table = layui.table;
		var form = layui.form;
		var util = layui.util;
		
		//所获得的 tableIns 即为当前容器的实例
		var tableIns = table.render({
		  elem: '#table'
		  ,url: 'page' //数据接口
		  ,page: true //开启分页
		  ,cols: [[ //表头
		     {type: "checkbox", fixed: 'left'}
		    ,{field: 'id', type: "numbers", title: 'ID', width: 60, sort: true}
		    ,{field: 'url', title: 'URL'}
		    ,{field: 'dwz_code', title: '短网址后缀'}
		    ,{field: 'dwz_name', title: '短网址名称', edit: "text"}
		    ,{field: 'elaborate_on', title: '详细说明', edit: "text"}
		    ,{field: 'create_time', title: '创建时间', sort: true, templet: function(d){
			    return util.toDateString(d.create_time)
		    }}
		    ,{field: 'update_time', title: '更新时间', sort: true, templet: function(d){
				return util.toDateString(d.update_time)
		   	}}
		    ,{title: '操作', width: '10%', fixed: 'right', align:'center', toolbar: ".td-manage"} //这里的toolbar值是模板元素的选择器
		  ]]
		  ,done: function(res, curr, count){
			    //如果是异步请求数据方式，res即为你接口返回的信息。
			    //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
				$("#count").text("共有数据："+ count +" 条");
		   }
		});
		
		//监听搜索
        form.on('submit(search)', function(obj){
			//表格重载
			tableIns.reload({
			  where: obj.field //设定异步数据接口的额外参数，任意设
			})
			return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        })
		
		//监听工具条
	    table.on('tool(table)', function(obj){ //注：tool是工具条事件名，table是table原始容器的属性 lay-filter="对应的值"
	      var data = obj.data; //获得当前行数据
	      var id = data.id;
	      var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	     
	      if(layEvent === 'edit'){ //编辑
	        x_admin_show('编辑','edit', id);
		  }else if (layEvent === 'del'){ //删除
	      	layer.confirm('确认要删除此行数据么', function(index){
	        	layer.close(index);
	        	var load_index = layer.load();
	          	//向服务端发送删除指令
	          	$.ajax({
	        		type: "delete",
	        		url: 'delete',
	        		contentType: "application/json",
	        		data: '[{"id": '+ id +'}]',
	        		success: function(result) {
		        		layer.close(load_index);
		        		if (result.flag) {
				            layer.msg('已删除!',{icon:1,time:2000});
					        obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
		        		}else {
		        			layer.alert(result.msg);
		        		}
	       			}
	      		})
	    	});
	  	  }
	  });
	  
	  table.on('edit(table)', function(obj){ //注：edit是固定事件名，table是table原始容器的属性 lay-filter="对应的值"
		  var id = obj.data.id;
		  var key = obj.field;
		  var value = obj.value;
		  layer.confirm('确认修改?', {icon: 3, title:'提示'}, function(index){
			  layer.close(index);
			  var load_index = layer.load();
	    	  $.ajax({
	    		  type: "put",
	    		  url: "update",
	    		  data: "id=" + id + "&" + key + "=" + value,
	    		  success: function(result){
	        		layer.close(load_index);
	        		if (result.flag) {
	        			layer.msg("修改成功", {icon: 6});
	        		}else {
	        			layer.alert(result.msg);
	        		}
	        	}
	    	  })
		  });
		});
	  });
	
      layui.use('laydate', function(){
        var laydate = layui.laydate;
        
        //执行一个laydate实例
        laydate.render({
          elem: '#start_date' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end_date' //指定元素
        });
      });

      function delAll (argument) {
        var data = tableCheck.getData();
  
        layer.confirm('确认要删除吗？'+data,function(index){
            //捉到所有被选中的，发异步进行删除
            layer.msg('删除成功', {icon: 1});
            $(".layui-form-checked").not('.header').parents('tr').remove();
        });
      }
    </script>
  </body>
</html>